/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package bw.news;

import com.sun.syndication.feed.synd.*;
import java.util.*;
import java.text.*;
import java.io.*;
import bw.util.*;

public class NewsItem implements Serializable
{
    static final long serialVersionUID = 2418416292543620919L;

    private String _title = "";
    private String _link = "";
    private String _description = "";
    private boolean _isNew = true;
    private Date _publishedDate = null;
    private boolean _isStarred = false;
    private boolean _allowStar = true;

    public NewsItem ()
    {
        //nothing
    }

    public static NewsItem fromSyndEntry ( SyndEntry entry )
    {
        NewsItem item = new NewsItem();

        item.setTitle( entry.getTitle() );        
        item.setLink( entry.getLink() );
        
        //note -- this might be null
        if ( entry.getPublishedDate() != null )
        {
            item.setPublishedDate( entry.getPublishedDate() );
        }

        String src = "";
        Iterator it = entry.getContents().iterator();
        while ( it.hasNext() )
        {
            SyndContent c = (SyndContent) it.next();
            src += c.getValue();
        }
        
        //sometimes the entry content might be in the description
        //this happens in an Atom feed if they use the <summary> tag 
        //instead of <content> to store the text. 
        if ( src.length() == 0 )
        {
            SyndContent c = entry.getDescription();
            if ( c != null )
            {
                src += c.getValue();
            }
        }

        //if there are CDATA tags, remove them
        int cdataInd = src.indexOf( HtmlCheck.CDATA_TAG_START );
        if ( cdataInd > -1 )
        {
            int cdataEnd = src.indexOf( HtmlCheck.CDATA_TAG_END );
            if ( cdataEnd > cdataInd )
            {
                String cdata = src.substring( cdataInd + HtmlCheck.CDATA_TAG_START.length(), 
                                              cdataEnd );
                String before = src.substring( 0, cdataInd );
                String after = src.substring( cdataEnd + HtmlCheck.CDATA_TAG_END.length() );
                src = before + cdata + after;
            }
        }

        src = ( new HtmlCheck() ).operate( src );

        item.setDescription( src );

        return item;
    }

    public String getTitle ()
    {
        return _title;
    }

    public void setNew ( boolean flag )
    {
        _isNew = flag;
    }

    public boolean isNew ()
    {
        return _isNew;
    }

    public void setTitle ( String title )
    {
        if ( title != null )
        {
            _title = title;
        }
    }

    public boolean hasTitle ()
    {
        return isNonEmpty( _title );
    }

    public void setPublishedDate ( Date date )
    {
        _publishedDate = date;
    }

    public Date getPublishedDate ()
    {
        return _publishedDate;
    }

    public String getPublishedDateStr ()
    {
        return ( _publishedDate == null ? "" : bw.util.Format.formatMilitary( _publishedDate ) );
    }

    public boolean hasPublishedDate ()
    {
        return ( _publishedDate != null );
    }

    public String getLink ()
    {
        return _link;
    }

    public void setLink ( String link )
    {
        if ( link != null )
        {
            _link = link;
        }
    }

    public boolean hasLink ()
    {
        return isNonEmpty( _link );
    }

    public String getDescription ()
    {
        return _description;
    }

    public void setDescription ( String desc )
    {
        if ( desc != null )
        {
            _description = desc;
        }
    }

    public boolean hasDescription ()
    {
        return isNonEmpty( _description );
    }

    public boolean isStarred () { return _isStarred; }
    public void setStarred ( boolean b ) { _isStarred = b; }

    public void setAllowStar ( boolean b ) { _allowStar = b; }
    public boolean allowsStar () { return _allowStar; }

    private boolean isNonEmpty ( String str )
    {
        return ( str != null && str.length() > 0 );
    }

    public String getId ()
    {
        //hash the title and description -- sometimes 2 news items will have
        //the same title so do both just in case. 
        return String.valueOf( ( getTitle() + getDescription() ).hashCode() ); 
    }

    public boolean equals ( Object obj )
    {
        NewsItem item = (NewsItem) obj;
        
        boolean equals = false;
        
        try {
            equals = ( item.getDescription().equals( getDescription() ) &&
                       item.getTitle().equals( getTitle() ) );
        }
        catch ( NullPointerException ex )
        {
            Log.getInstance().write( "Null Pointer in NewsItem.equals()", ex );
        }

        return equals;
    }

    public String toString ()
    {
        return _title;
    }
}